﻿using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Web.Security;
using Artech.PetShop.Common;
using Artech.PetShop.Infrastructures.Service.Interface;

namespace Artech.PetShop.Infrastructures
{
    public class RemoteMembershipProvider : MembershipProvider
    {
        private bool _enablePasswordReset;
        private bool _enablePasswordRetrieval;
        private int _maxInvalidPasswordAttempts;
        private int _minRequiredNonAlphanumericCharacters;
        private int _minRequiredPasswordLength;
        private int _passwordAttemptWindow;
        private MembershipPasswordFormat _passwordFormat;
        private string _passwordStrengthRegularExpression;
        private bool _requiresQuestionAndAnswer;
        private bool _requiresUniqueEmail;

        public IMembershipService MembershipProxy
        { get; private set; }

        public override string ApplicationName
        { get; set; }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            return this.MembershipProxy.ChangePassword(username, oldPassword, newPassword);
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            return this.MembershipProxy.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer);
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            return this.MembershipProxy.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            return this.DeleteUser(username, deleteAllRelatedData);
        }

        public override bool EnablePasswordReset
        {
            get { return this._enablePasswordReset; }
        }

        public override bool EnablePasswordRetrieval
        {
            get { return this._enablePasswordRetrieval; }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            return this.MembershipProxy.FindUsersByEmail(emailToMatch, pageIndex, pageSize, out totalRecords);
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            return this.MembershipProxy.FindUsersByName(usernameToMatch, pageIndex, pageSize, out totalRecords);
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            return this.MembershipProxy.GetAllUsers(pageIndex, pageSize, out totalRecords);
        }

        public override int GetNumberOfUsersOnline()
        {
            return this.MembershipProxy.GetNumberOfUsersOnline();
        }

        public override string GetPassword(string username, string answer)
        {
            return this.GetPassword(username, answer);
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            return this.GetUser(username, userIsOnline);
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            return this.MembershipProxy.GetUser(providerUserKey, userIsOnline);
        }

        public override string GetUserNameByEmail(string email)
        {
            return this.MembershipProxy.GetUserNameByEmail(email);
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { return this._maxInvalidPasswordAttempts; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return this._minRequiredNonAlphanumericCharacters; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return this._minRequiredPasswordLength; }
        }

        public override int PasswordAttemptWindow
        {
            get { return this._passwordAttemptWindow; }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { return this._passwordFormat; }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { return this._passwordStrengthRegularExpression; }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { return this._requiresQuestionAndAnswer; }
        }


        public override bool RequiresUniqueEmail
        {
            get { return this._requiresUniqueEmail; }
        }

        public override string ResetPassword(string username, string answer)
        {
            return this.MembershipProxy.ResetPassword(username, answer);
        }

        public override bool UnlockUser(string userName)
        {
            return this.MembershipProxy.UnlockUser(userName);
        }

        public override void UpdateUser(MembershipUser user)
        {
            this.MembershipProxy.UpdateUser(user);
        }

        public override bool ValidateUser(string username, string password)
        {
            return this.MembershipProxy.ValidateUser(username, password);
        }

        //......

        public override void Initialize(string name, NameValueCollection config)
        {
            if (!config.AllKeys.Contains<string>("endpoint"))
            {
                throw new ConfigurationErrorsException("Missing the mandatory \"endpoint\" configuraiton property.");
            }

            this.MembershipProxy = ServiceProxyFactory.Create<IMembershipService>(config["endpoint"]);
            base.Initialize(name, config);
            MembershipConfigData configData = this.MembershipProxy.GetMembershipConfigData();
            this.ApplicationName = configData.ApplicationName;
            this._enablePasswordReset = configData.EnablePasswordReset;
            this._enablePasswordRetrieval = configData.EnablePasswordRetrieval;
            this._maxInvalidPasswordAttempts = configData.MaxInvalidPasswordAttempts;
            this._minRequiredNonAlphanumericCharacters = configData.MinRequiredNonAlphanumericCharacters;
            this._minRequiredPasswordLength = configData.MinRequiredPasswordLength;
            this._passwordAttemptWindow = configData.PasswordAttemptWindow;
            this._passwordFormat = configData.PasswordFormat;
            this._passwordStrengthRegularExpression = configData.PasswordStrengthRegularExpression;
            this._requiresQuestionAndAnswer = configData.RequiresQuestionAndAnswer;
            this._requiresUniqueEmail = configData.RequiresUniqueEmail;
        }
    }
}